<template>
  <!-- 实时还款查询 -->
  <section class="page repayment-record-query">
    <data-form :model="searchModel" @onSearch="refreshData" :page="pageService" @handleResetForm="handleResetForm">
      <template slot="inputs">
        <el-form-item label="组织机构" prop="deptCodes">
          <el-cascader v-model="searchModel.deptCodes" :options="departmentData" :props="{value:'deptCode',label:'deptName'}" @change="changeDept" change-on-select :show-all-levels="false" clearable></el-cascader>
        </el-form-item>
        <el-form-item label="当前催收员" prop="collectorId">
          <el-select v-model="searchModel.collectorId" placeholder="请选择" clearable>
            <el-option v-for="item in userDataSet" :key="item.id" :label="item.realName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="案件金额" prop="caseMoney">
          <number-range v-model="searchModel.caseMoney" :isInteger="true"></number-range>
        </el-form-item>
      </template>
    </data-form>

    <data-box :data="dataSet" @onPageChange="queryRepaymentRecord" :page="pageService">
      <template slot="columns">
        <!--数据列区域-->
        <el-table-column prop="caseNumber" label="订单编号" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip></el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号码" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="caseMoney" label="案件金额(元)" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="overdueAmount" label="欠款金额(元)" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="repayAmount" label="还款金额" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="entryTime" label="还款时间" :formatter="(row) => $filter.dateTimeFormat(row.entryTime)" :min-width="$helper.getColumnWidth(7)" show-overflow-tooltip>
        </el-table-column>
      </template>
      <p slot="footer">{{`查询结果统计：当前还款总金额 ${$filter.toThousands(totalInfo.totalAmount)}元`}}</p>
    </data-box>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Auth, Layout, Dependencies } from "~/core/decorator";
import { PageService } from "~/utils/page.service";
import DataForm from "~/components/common/data-form.vue";
import DataBox from "~/components/common/data-box.vue";
import { SortService } from "~/utils/sort.service";
import NumberRange from "~/components/common/number-range.vue";
import { SynthesizeQueryService } from "~/services/domain-service/synthesize-query.service";
import { namespace, Getter, Mutation } from "vuex-class";
import { UserService } from "~/services/management-service/user.service";

const CollectionExecutionModule = namespace('collection-execution');

@Layout("workspace")
@Component({
  components: {


    NumberRange
  }
})
export default class RepaymentRecordQuery extends Vue {

  @Dependencies(PageService) private pageService: PageService;

  @Dependencies(SortService) private sortService: SortService;

  @Dependencies(SynthesizeQueryService)
  private synthesizeQueryService: SynthesizeQueryService;

  @Dependencies(UserService)
  private userService: UserService;

  @Getter
  private departmentData;

  @Getter
  private isSupperAdmin;

  private totalInfo: any = {};

  private searchModel: any = {
    deptCodes: [],
    collectorId: "",
    caseMoney: { min: '', max: '' }
  };

  private dataSet: any[] = [];

  private userDataSet = [];

  private userModel: any = {
    deptCode: ""
  };
  private queryTotalAmount() {
    this.synthesizeQueryService
      .getTotalRepayment(this.searchModel)
      .subscribe(data => (this.totalInfo = data));
  }

  private queryRepaymentRecord() {
    this.synthesizeQueryService
      .queryRepaymentRecord(this.searchModel, this.pageService)
      .subscribe(data => (this.dataSet = data));
  }

  private refreshData() {
    this.queryRepaymentRecord();
    this.queryTotalAmount();
  }

  private handleResetForm() {
    this.searchModel.caseMoney = { min: '', max: '' }
  }

  private mounted() {
    if (!(this.departmentData[0].deptCode)) {
      return;
    }
    this.searchModel.deptCodes.push(this.departmentData[0].deptCode);
    this.getAllUserByDeptCode();
    this.refreshData();
  }

  private getAllUserByDeptCode() {
    if (this.searchModel.deptCodes.length === 0) {
      return;
    }
    this.userModel.deptCode = this.searchModel.deptCodes[this.searchModel.deptCodes.length - 1]
    this.userService
      .getAllUsers(this.userModel, this.pageService)
      .subscribe(data => {
        this.userDataSet = data;
      });
  }
  private changeDept() {
    this.getAllUserByDeptCode()
  }
}
</script>
<style lang="less">
</style>
